<?php


namespace app\api\controller;


use app\admin\model\GoodsOrder;
use app\admin\model\GoodsType;
use app\admin\model\User;
use think\Request;

class Goods extends Base
{
    public function getGoodsList(Request $request){
        $res = [];
        $data = $request->post();
        $page = $request->get();
        $goods = new \app\admin\model\Goods();
        if(!isset($page['page']) ){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $where['status'] = 1;

        if($data['type'] == 1){
            $where['type_id'] = $data['goods_type'];
        }if($data['type'] == 2){
            $where['text'] = ['like',"%".$data['selected'].'%'];
        }
        $rs = $goods->getPageAll($where);

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getGoodsDetail(Request $request){
        $res = [];
        $data = $request->post();
        $goods = new \app\admin\model\Goods();
        if(!isset($data['id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $where['id'] = $data['id'];
        $rs = $goods->getOne($where);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getGoodsType(){
        $res = [];

        $goodsType = new GoodsType();
        $where['status'] = 1;
        $rs = $goodsType->getAll($where);
        $list['left'] = $rs;

        $where1['hot'] = ['neq',0];
        $rs1 = $goodsType->getHotAll($where1);
        $list['rightTop'] = $rs1;

        $goods = new \app\admin\model\Goods();
        $where2['sort'] = ['neq',0];
        $rs2 = $goods->getHot($where2);
        $list['rightDown'] = $rs2;

        if($rs == null){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '获取失败';
        }else{
            $res['data']['code'] = 1;
            $res['data']['msg'] = '获取成功';
            $res['data']['list'] = $list;
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getTypeGoodsList(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['type'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $goods = new \app\admin\model\Goods();
        $where['type_id'] = $data['type'];
        $rs = $goods->getAll($where);
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '查询成功';
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '查询失败';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }


    public function addGoodsOrder(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['goods_id']) or !isset($data['num']) or !isset($data['demand'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new User();
        $userWhere['id'] = $data['user_id'];
        $userInfo = $user->getOne($userWhere);
        if(!$userInfo){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '用户不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $goods = new \app\admin\model\Goods();
        $where['id'] = $data['goods_id'];
        $goodsInfo = $goods->getOne($where);
        if($goodsInfo){
            $goodsOrder = new GoodsOrder();

            $goodsOrderWhere['user_id'] = $data['user_id'];
            $goodsOrderWhere['g_id'] = $goodsInfo['id'];
            $goodsOrderWhere['status'] = 0;
            $getOrder = $goodsOrder->getOne($goodsOrderWhere);
            if($getOrder){
                $res['data']['code'] = 0;
                $res['data']['msg'] = '订单已存在';
                $res['status'] = 200;
                echo json_encode($res);die;
            }

            $addData['user_id'] = $data['user_id'];
            $addData['g_id'] = $goodsInfo['id'];
            $addData['name'] = $goodsInfo['name'];
            $addData['num'] = $data['num'];
            $addData['demand'] = $data['demand'];
            $addData['order'] = time().rand(00000,99999);
            $addData['create_time'] = time();

            $rs = $goodsOrder->add($addData);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '下单成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '下单失败';
            }
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '产品不存在';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }

    public function getGoodsOrder(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $goodsOrder = new GoodsOrder();
        $where['user_id'] = $data['user_id'];
        $rs = $goodsOrder->getAll($where);

        $goods = new \app\admin\model\Goods();

        $list = [];
        $list['wgt'] = [];
        $list['zzgt'] = [];
        $list['zzz'] = [];
        $list['ywc'] = [];
        foreach ($rs as $k){
            if($k['status'] == 0){
                $list['wgt'][] =$k;
            }else if($k['status'] == 1){
                $list['zzgt'][] =$k;
            }else if($k['status'] == 2){
                $list['zzz'][] =$k;
            }else{
                $list['ywc'][] =$k;
            }
        }

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '查询成功';
            $res['data']['list'] = $list;
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '查询失败';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }
}